package com.bjpowernode.boss;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bjpowernode.boss.mvc.models.dic.DicItem;
import com.bjpowernode.boss.persist.PayWayGroup;
import com.bjpowernode.boss.persist.domain.DicPayWay;
import com.bjpowernode.boss.persist.mapper.DicPayTypeMapper;
import com.bjpowernode.boss.persist.mapper.DicPayWayMapper;
import jakarta.annotation.Resource;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.scheduling.annotation.EnableAsync;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@MapperScan("com.bjpowernode.boss.persist.mapper")
@SpringBootApplication
@EnableAsync
public class PowerBossApplication implements CommandLineRunner {



    public static void main(String[] args) {
        SpringApplication.run(PowerBossApplication.class, args);

    }
    //支付类型分组集合
    public static Map<String, List<DicItem>> PAY_TYPE_GROUP_DIC = new HashMap<>();

    public static Map<String, String>  PAY_WAY_DIC = new HashMap<>();

    //entry = (wayCode + ":" + typeCode, typeName);
    public static Map<String, String> PAY_TYPE_DIC = new HashMap<>();

    @Resource
    private ApplicationContext applicationContext;

    @Override
    public void run(String... args) throws Exception {

        DicPayTypeMapper dicPayTypeMapper = applicationContext.getBean(DicPayTypeMapper.class);
        DicPayWayMapper dicPayWayMapper = applicationContext.getBean(DicPayWayMapper.class);

        List<DicPayWay> dicPayWays = dicPayWayMapper.selectList(new QueryWrapper<>());
        dicPayWays.forEach(dicPayWay -> {
            PAY_WAY_DIC.put(dicPayWay.getPayWayCode(), dicPayWay.getPayWayName());
        });

        List<PayWayGroup> payWayGroupList = dicPayTypeMapper.selectGroupByPayWay();
        payWayGroupList.forEach( payWayGroup -> {
            String payWayCode = payWayGroup.getPayWayCode();
            String payTypes = payWayGroup.getPayTypes();
            String[] payTypeList = payTypes.split(";");
            List<DicItem> dicItemList = new ArrayList<>();
            for (String s : payTypeList) {
                String[] split = s.split("#");
                DicItem dicItem = DicItem.builder().code(split[0]).label(split[1]).build();
                dicItemList.add(dicItem);
                PAY_TYPE_DIC.put(payWayCode + ":" + split[0], split[1]);
            }
            PAY_TYPE_GROUP_DIC.put(payWayCode, dicItemList);
        });
    }
}
